package com.youlai.boot.modules.game.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.game.model.entity.GamePointsHistory;
import com.youlai.boot.modules.game.model.form.GamePointsForm;
import com.youlai.boot.modules.game.model.form.GameUserProfileForm;
import com.youlai.boot.modules.game.model.query.GameLeaderboardQuery;
import com.youlai.boot.modules.game.model.vo.GameLeaderboardVO;
import com.youlai.boot.modules.game.service.GameLeaderboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 游戏排行榜控制器
 *
 * @author 有来技术团队
 */
@Tag(name = "游戏排行榜接口")
@RestController
@RequestMapping("/api/v1/game/leaderboard")
@RequiredArgsConstructor
public class GameLeaderboardController {

    private final GameLeaderboardService gameLeaderboardService;

    @Operation(summary = "获取排行榜列表")
    @GetMapping
    public Result<List<GameLeaderboardVO>> getLeaderboard(
            @ParameterObject GameLeaderboardQuery queryParams
    ) {
        List<GameLeaderboardVO> leaderboard = gameLeaderboardService.getLeaderboard(queryParams);
        return Result.success(leaderboard);
    }

    @Operation(summary = "获取用户排名")
    @GetMapping("/user-rank")
    public Result<GameLeaderboardVO> getUserRank(
            @Parameter(description = "时间段") @RequestParam(required = false) String period
    ) {
        GameLeaderboardVO userRank = gameLeaderboardService.getUserRank(period);
        return Result.success(userRank);
    }

    @Operation(summary = "更新用户积分")
    @PostMapping("/points")
    public Result<Boolean> updatePoints(
            @Validated @RequestBody GamePointsForm formData
    ) {
        boolean result = gameLeaderboardService.updatePoints(formData);
        return Result.judge(result);
    }

    @Operation(summary = "更新用户资料")
    @PutMapping("/user-profile")
    public Result<Boolean> updateUserProfile(
            @Validated @RequestBody GameUserProfileForm formData
    ) {
        boolean result = gameLeaderboardService.updateUserProfile(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户积分历史")
    @GetMapping("/points/history")
    public PageResult<GamePointsHistory> getPointsHistory(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return gameLeaderboardService.getPointsHistory(pageNum, pageSize);
    }
} 